<?php
namespace Base\Controller;

use Admin\Model\BaseinfoVersionModel;
use Admin\Model\DepartmentinfoModel;
use Admin\Model\MenuModel;
use Admin\Model\MessageModel;
use Admin\Model\UserGroupModel;
use Admin\Model\UserModel;
use Library\jPush;
use Org\Net\IpLocation;
use Think\Controller;

class BaseAdminController extends Controller
{
    protected $user;
    protected $userGroup;
    public function __construct()
    {
        parent::__construct();
        $this->addbaseinfo();
        $user = $this->getUser();
        if (!empty($user)) {
            $this->user = $user;
            $this->assign('user', $user);
        }
    }
    //验证密码
    protected function checkPwd(){
        //判断密码是否纯数字
        if(is_numeric($_POST['passwd']) and strlen($_POST['passwd'])<8){
            $this->error('纯数字密码最少8位！');
        }
    }


    /**
     * @return bool
     * 当前是否登录
     */
    protected function isLogin()
    {
        return $this->getUser() ? true : false;
    }

    /**
     * 数据库信息添加
     */
  protected  function  addbaseinfo(){
      $action = hump2underline(ACTION_NAME);
      $controller = hump2underline(CONTROLLER_NAME);
      $BaseinfoVersionModel =new BaseinfoVersionModel();
      if(($action=='add') or ($action=='edit')  or ($action=='del') or ($action=='recover') or ($action=='reviewed')){
          $result=$BaseinfoVersionModel->where(array('name'=>$controller))->find();
          if($result){
              $data['version']=$result['version']+1;
              $data['updatetime']=date("Y-m-d H:i:s");
              $BaseinfoVersionModel->where(array('name'=>$controller))->save($data);
          }else{
              $data['version']=1;
             $ids= $BaseinfoVersionModel->order('id desc')->find();
              $data['id']=$ids['id']+1;
              $data['status']=1;
              $data['name']=$controller;
              $data['updatetime']=date("Y-m-d H:i:s");
              $BaseinfoVersionModel->add($data);
          }
      }
  }
    /**
     * 判断是否登录
     */
    protected function getUser()
    {
        return session('user');
    }

    /**
     * @return mixed
     * 获取菜单
     */
    protected function getMenuList()
    {
        $userGroupModel = new UserGroupModel();
        $userGroup = $userGroupModel->getOne($this->user['user_group_id']);
        $menu = json_decode($userGroup['menu']);
        $model = new MenuModel();
        $data = $model->relation(true)->where('status=1 and menu_id < 1 or menu_id is null')->select();
        foreach ($data as $k => $v) {
            if (!in_array($v['id'], $menu)) unset($data[$k]);
            foreach ($v['menu'] as $kk => $vv) {
                if (!in_array($vv['id'], $menu)) unset($data[$k]['menu'][$kk]);
            }
        }
        return $data;
    }

    //查询用户信息
    public function getUserInfo(){
        $userModel = new UserModel();
        $user = $userModel->getOne($this->user['id']);
        return $user;
    }

//    /**
//     * @param $user_id    ---用户id
//     * @param $user_type  ---用户类型(1.患者，2.医生)
//     * @param $title ---消息
//     * @param $data  ---数据
//     * @param $device  ---设备类型
//     *保存用户消息
//     */
//    protected function addMsg($user_id='',$user_type='',$title='',$data=array(),$device=''){
//        //添加消息
//        $msg_info = M('msg_manager')->where(array('controller'=>CONTROLLER_NAME,'action'=>ACTION_NAME,'is_push'=>1,'status'=>1))->field('title,describe')->find();
//        if (!empty($msg_info)){
//            //获取用户信息
//            $userInfo['user_id'] = empty($user_id) ? session('user_id') : $user_id;
//            $userInfo['user_type'] = empty($user_type) ? session('user_type') : $user_type;
//            $title = empty($title) ? $msg_info['title'] : $title;  //消息
//            $data = empty($data) ? '' : $data;   //数据
//            if (!empty($userInfo['user_id'])){
//                $userInfo['title'] = $title;
//                $userInfo['content'] = $msg_info['describe'];
//                $userInfo['msg_data'] = json_encode($data);
//                $msgModel = new MessageModel();
//                $msgModel->add($msgModel->create($userInfo));
//                //获取用户名
//                $jPush = new jPush();
//                $jPush->pushMsg($userInfo['user_id'],$userInfo['user_type'],
//                    array('controller'=>CONTROLLER_NAME,'action'=>ACTION_NAME),
//                    $title,$title,$data,$device);
//            }
//        }
//    }
    function  getdepartmentList(){
        $deparmentmodel = new DepartmentinfoModel();
         return $deparmentmodel->where('status=1')->select();
    }

}